package com.ylz.sjzx.sio.common.core.utils;

import com.ylz.sjzx.sio.common.core.annotation.MessageField;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @author wiqi
 * @version 1.0
 * @date 2020/12/10
 */
public class DeclareFieldUtil {
    public static List<Field> getOrderedField(Class aClass) {

        List<Field> tempFields = new ArrayList<>();
        tempFields.addAll(Arrays.asList(aClass.getSuperclass().getDeclaredFields()));
        tempFields.addAll(Arrays.asList(aClass.getDeclaredFields()));
        //用来存放所有的属性域
        List<Field> fieldList = new ArrayList<>();
        // 过滤带有注解的Field
        for (Field f : tempFields) {
            if (f.getAnnotation(MessageField.class) != null) {
                fieldList.add(f);
            }
        }
        fieldList.sort(Comparator.comparingInt(m -> m.getAnnotation(MessageField.class).order()));
        return fieldList;
    }

    public static boolean isStandardProperty(Class clazz) {
        return
                clazz.isAssignableFrom(Byte.class) ||
                        clazz.isAssignableFrom(Short.class) ||
                        clazz.isAssignableFrom(Integer.class) ||
                        clazz.isAssignableFrom(Long.class) ||
                        clazz.isAssignableFrom(Float.class) ||
                        clazz.isAssignableFrom(Double.class) ||
                        clazz.isAssignableFrom(Character.class) ||
                        clazz.isAssignableFrom(String.class) ||
                        clazz.isAssignableFrom(Boolean.class) ||

                        clazz.isAssignableFrom(byte.class) ||
                        clazz.isAssignableFrom(short.class) ||
                        clazz.isAssignableFrom(int.class) ||
                        clazz.isAssignableFrom(long.class) ||
                        clazz.isAssignableFrom(float.class) ||
                        clazz.isAssignableFrom(double.class) ||
                        clazz.isAssignableFrom(char.class) ||
                        clazz.isAssignableFrom(boolean.class)
                ;
    }
}
